import {request} from "@/assets/js/utils/request";
export default{
	data() {
		return {
			
		}
	},
	methods: {
		formattedDate(params) {
            const date = new Date(params);
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, '0');
            const day = String(date.getDate()).padStart(2, '0');
            return `${year}-${month}-${day}`;
        },
		exportClick(type) {
			let fileName = ''
			if(type == 'keyan') {
				fileName = '科研项目'
			}
			if(type == 'jiaoxue') {
				fileName = '教学课程'
			}
			if(type == 'xuewei') {
				fileName = '学位论文和创新实践'
			}
			if(type == 'prp') {
				fileName = 'PRP项目'
			}
			if(type == 'chuangxin') {
				fileName = '大学生创新创业训练计划'
			}
            request({
                url: '/manager/business/expExcel.jsp?sPage=businessInfo',
                method: 'get'
            }).then(res => {
                if(res.code == 200) {
                    let exceldata = this.getBase64Type("xls") + res.data
                    let ex = `${fileName}文件下载.xls`
                    this.downloadFileByBase64(exceldata,ex)
                }
            })
        },
		//将base64转换为blob
		dataURLtoBlob(dataurl) {
			var arr = dataurl.split(","),
				mime = arr[0].match(/:(.*?);/)[1],
				bstr = atob(arr[1]),
				n = bstr.length,
				u8arr = new Uint8Array(n);
			while (n--) {
				u8arr[n] = bstr.charCodeAt(n);
			}
			return new Blob([u8arr], { type: mime });
		},
		// * desc: 下载方法
		// * @param url  ：返回数据的blob对象或链接
		// * @param fileName  ：下载后文件名标记
		downloadFile(url, name) {
			var a = document.createElement("a");
			a.setAttribute("href", url);
			a.setAttribute("download", name);
			a.setAttribute("target", "_blank");
			let clickEvent = document.createEvent("MouseEvents");
			clickEvent.initEvent("click", true, true);
			a.dispatchEvent(clickEvent);
		},
		// * desc: 下载参数入口
		// * @param base64  ：返回数据的blob对象或链接
		// * @param fileName  ：下载后文件名标记
		downloadFileByBase64(base64, fileName) {
			var myBlob = this.dataURLtoBlob(base64);
			var myUrl = URL.createObjectURL(myBlob);
			this.downloadFile(myUrl, fileName);
		},
		//根据文件后缀 获取base64前缀不同
		getBase64Type(type) {
			switch (type) {
				case 'txt':
					return 'data:text/plain;base64,';
				case 'doc':
					return 'data:application/msword;base64,';
				case 'docx':
					return 'data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,';
				case 'xls':
					return 'data:application/vnd.ms-excel;base64,';
				case 'xlsx':
					return 'data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64,';
				case 'pdf':
					return 'data:application/pdf;base64,';
				case 'pptx':
					return 'data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,';
				case 'ppt':
					return 'data:application/vnd.ms-powerpoint;base64,';
				case 'png':
					return 'data:image/png;base64,';
				case 'jpg':
					return 'data:image/jpeg;base64,';
				case 'gif':
					return 'data:image/gif;base64,';
				case 'svg':
					return 'data:image/svg+xml;base64,';
				case 'ico':
					return 'data:image/x-icon;base64,';
				case 'bmp':
					return 'data:image/bmp;base64,'; 
				case 'rar':
					return 'data:application/octet-stream;base64,';
				case 'zip':
					return 'data:application/x-zip-compressed;base64,';
			}
		},
		//附上获取文件后缀的方法
		getType(file) {
			var filename = file;
			var index1 = filename.lastIndexOf(".");
			var index2 = filename.length;
			var type = filename.substring(index1 + 1, index2);
			return type;
		},
		hasPermission(permission) {
            let str = sessionStorage.getItem('rightCodes')
            let rightCodes = JSON.parse(str)
            return rightCodes.includes(permission);
        },
		goBack() {
			this.$router.back()
		}
	}
}